<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class IndexController extends BaseController {
	
	public function _initialize(){
		parent::_initialize();
		if($this->isLogedIn()){
			$this->assign('isAdmin',1);
			$this->assign('username',session(BaseController::SESSION_NAME_KEY));
		}
		
	}
    public function index(){
        $this->display();
    }
	
	public function login(){
		$username = I('post.username');
		$password = I('post.password');
		if($this->check($username,$password)){
			session(BaseController::SESSION_ADMIN_KEY,$password);
			session(BaseController::SESSION_NAME_KEY,$username);
			$this->redirect('admin');
		}else{
			$this->redirect('index');
		}
	}
	
	private function check($username,$password){
		return $username === C('APP_USER') && $password === C('APP_PASSWORD');
	}
	
	private function isLogedIn(){
		return session(BaseController::SESSION_ADMIN_KEY) === C('APP_PASSWORD');
	}
	
	public function logout(){
		if($this->isLogedIn()){
			session(BaseController::SESSION_ADMIN_KEY,null);
			session(BaseController::SESSION_NAME_KEY,null);
			$this->redirect('index');
		}
	}
	
	public function admin(){
		if(!$this->isLogedIn()){
			$this-redirect('index');
		}
		// big calender here
		$this->display();
	}
	
	public function deleteService(){
		if(!$this->isLogedIn()){
			exit('-99');
		}
		$tid = I('post.tid');
		if(!is_numeric($tid)){
			exit('-10');
		}
		$result = M('termine')->where('termine_id='.$tid)->delete();
		if($result !== false){
			echo '1';
		}else{
			echo '0';
		}
	}
	
	public function resetServiceTime(){
		if(!$this->isLogedIn()){
			exit('-99');
		}
		$tid = I('post.tid');
		$start = I('post.start');
		$end = I('post.end');
		if(!is_numeric($tid) || !is_numeric($start) || !is_numeric($end)){
			exit('-10');
		}
		$data = array();
		$data['termine_id'] = $tid;
		$data['start'] = $start;
		$data['end'] = $end - 1;
		$result = M('termine')->data($data)->save();
		if($result !== false){
			echo '1';
		}else{
			echo '0';
		}
	}
	
	public function getModelForm(){
		if(!$this->isLogedIn()){
			exit();
		}
		$tid = I('get.tid');
		if(is_numeric($tid)){
			$termin = M('termine')->join('jp_kunden on jp_kunden.kunden_id=jp_termine.kunden_id')->join('jp_therapie on jp_therapie.therapie_id=jp_termine.therapie_id')->field('jp_kunden.*,jp_therapie.*,jp_termine.*')->where('jp_termine.termine_id='.$tid)->select();
			$termin = $termin[0];
			$termin['end'] = $termin['end'] + 1;
			$this->assign('termin',$termin);
			
			$start_date_show = date('d/m/Y',$termin['start']);
			$start_date = date('Y-m-d',$termin['start']);
			$this->assign('start_date_show',$start_date_show);
			$this->assign('start_date',$start_date);
		}
		
		$st = I('get.st');
		if(is_numeric($st)){
			$start_date_show = date('d/m/Y',$st);
			$start_date = date('Y-m-d',$st);
			$this->assign('start_date_show',$start_date_show);
			$this->assign('start_date',$start_date);
		}
		$therapies = M('therapie')->where('active=1')->select();
		$this->assign('therapies',$therapies);
		
		$this->assign('isAdmin',1);
		$this->display('_modelForm');
	}
	
	public function getTherapiePlan(){
		if(!$this->isLogedIn()){
			exit('0');
		}
		$start = $_GET['start'];
		$end = $_GET['end'];
		if(!is_numeric($start) || !is_numeric($end)){
			exit('-1');
		}
		$eventrows = M('termine')->join('jp_kunden on jp_kunden.kunden_id=jp_termine.kunden_id')->join('jp_therapie on jp_therapie.therapie_id=jp_termine.therapie_id')->field('jp_kunden.*,jp_therapie.*,jp_termine.*')->where('jp_termine.start <='.$end.' and jp_termine.end >='.$start)->select();
		$events = array();
		foreach($eventrows as $row){
			$event = array();
			$event['allDay'] = 0;
			$event['id'] = $row['termine_id'];
			$event['start'] = $row['start'];
			$event['end'] = $row['end'];
			$event['end'] +=1;
			$event['title'] = $row['therapie_name'];
			$event['description'] = $this->getDescriptionFromContent($row);
			$event['editable'] = 1;
			$events[] = $event;
		}
		echo json_encode($events);
		
	}
	
	private function getDescriptionFromContent($content){
		$result = 'kunde: '.$content['name'].', '.$content['vorname'].'<br/>';
		$result .= 'start: '.date('Y-m-d H:i',$content['start']).'<br/>';
		$result .= 'end: '.date('Y-m-d H:i',$content['end']).'<br/>';
		return $result;
	}
}
?>